package com.klangner.atarigo.model;

import junit.framework.TestCase;

import com.klangner.game.board.IBoard;
import com.klangner.game.board.impl.SimpleBoard;


public class GroupingAlgorithmTest extends TestCase{

	/**
	 * no group
	 */
	public void testNoGroup() {
		GroupingAlgorithm	ga;
		IBoard 		board = new SimpleBoard(9);
		
		ga = new GroupingAlgorithm(board);
		assertEquals(0, ga.getGroups().size());
	}
	
	/**
	 * single group
	 */
	public void testSingleStone() {
		GroupingAlgorithm	ga;
		IBoard 		board = new SimpleBoard(9);
		
		board.setStone(1, 2, IBoard.BLACK_STONE);
		ga = new GroupingAlgorithm(board);
		assertEquals(1, ga.getGroups().size());
	}
	
	/**
	 * single group
	 */
	public void test2Stones() {
		GroupingAlgorithm	ga;
		IBoard 		board = new SimpleBoard(9);
		
		board.setStone(1, 2, IBoard.BLACK_STONE);
		board.setStone(2, 2, IBoard.BLACK_STONE);
		ga = new GroupingAlgorithm(board);
		assertEquals(1, ga.getGroups().size());
		StonesGroup group = ga.getGroups().iterator().next();
		assertEquals(2, group.getStones().size());
		
	}
	
	/**
	 * single group
	 */
	public void test2Stones2() {
		GroupingAlgorithm	ga;
		IBoard 		board = new SimpleBoard(9);
		
		board.setStone(2, 1, IBoard.BLACK_STONE);
		board.setStone(2, 2, IBoard.BLACK_STONE);
		ga = new GroupingAlgorithm(board);
		assertEquals(1, ga.getGroups().size());
		StonesGroup group = ga.getGroups().iterator().next();
		assertEquals(2, group.getStones().size());
		
	}
	
	/**
	 * ..x..
	 * .xox.
	 * ..x..
	 */
	public void test5groups() {
		GroupingAlgorithm	ga;
		IBoard 		board = new SimpleBoard(9);
		
		board.setStone(1, 2, IBoard.BLACK_STONE);
		board.setStone(3, 2, IBoard.BLACK_STONE);
		board.setStone(2, 1, IBoard.BLACK_STONE);
		board.setStone(2, 3, IBoard.BLACK_STONE);
		board.setStone(2, 2, IBoard.WHITE_STONE);

		ga = new GroupingAlgorithm(board);
		assertEquals(5, ga.getGroups().size());
	}
	
	/**
	 * ..x..
	 * .xoo.
	 * ..x..
	 */
	public void test4Groups() {
		GroupingAlgorithm	ga;
		IBoard 		board = new SimpleBoard(9);
		
		board.setStone(2, 1, IBoard.BLACK_STONE);
		board.setStone(2, 3, IBoard.BLACK_STONE);
		board.setStone(1, 2, IBoard.BLACK_STONE);
		board.setStone(3, 2, IBoard.WHITE_STONE);
		board.setStone(2, 2, IBoard.WHITE_STONE);

		ga = new GroupingAlgorithm(board);
		assertEquals(4, ga.getGroups().size());
	}
	
	/**
	 * .xxx.
	 * .xOox
	 * ..xx.
	 */
	public void test4Groups2() {
		GroupingAlgorithm	ga;
		IBoard 		board = new SimpleBoard(9);
		
		board.setStone(1, 1, IBoard.BLACK_STONE);
		board.setStone(2, 1, IBoard.BLACK_STONE);
		board.setStone(2, 3, IBoard.BLACK_STONE);
		board.setStone(1, 2, IBoard.BLACK_STONE);
		board.setStone(3, 1, IBoard.BLACK_STONE);
		board.setStone(3, 3, IBoard.BLACK_STONE);
		board.setStone(4, 2, IBoard.BLACK_STONE);
		
		board.setStone(3, 2, IBoard.WHITE_STONE);
		board.setStone(2, 2, IBoard.WHITE_STONE);

		ga = new GroupingAlgorithm(board);
		assertEquals(4, ga.getGroups().size());
	}
	
	/**
	 * ..xx
	 * .xx.
	 * ....
	 */
	public void test1Groups2() {
		GroupingAlgorithm	ga;
		IBoard 		board = new SimpleBoard(9);
		
		board.setStone(2, 1, IBoard.BLACK_STONE);
		board.setStone(3, 1, IBoard.BLACK_STONE);
		board.setStone(1, 2, IBoard.BLACK_STONE);
		board.setStone(2, 2, IBoard.BLACK_STONE);

		ga = new GroupingAlgorithm(board);
		assertEquals(1, ga.getGroups().size());
		StonesGroup group = ga.getGroups().iterator().next();
		assertEquals(4, group.getStones().size());
	}
	
	/**
	 * ..xx
	 * .xxx
	 * ....
	 */
	public void test1Groups3() {
		GroupingAlgorithm	ga;
		IBoard 		board = new SimpleBoard(9);
		
		board.setStone(2, 1, IBoard.BLACK_STONE);
		board.setStone(3, 1, IBoard.BLACK_STONE);
		board.setStone(1, 2, IBoard.BLACK_STONE);
		board.setStone(2, 2, IBoard.BLACK_STONE);
		board.setStone(3, 2, IBoard.BLACK_STONE);

		ga = new GroupingAlgorithm(board);
		assertEquals(1, ga.getGroups().size());
		StonesGroup group = ga.getGroups().iterator().next();
		assertEquals(5, group.getStones().size());
	}
	
	/**
	 * ....
	 * ..xx
	 * .xx.
	 * ....
	 */
	public void testLiberties() {
		GroupingAlgorithm	ga;
		IBoard 		board = new SimpleBoard(9);
		
		board.setStone(2, 1, IBoard.BLACK_STONE);
		board.setStone(3, 1, IBoard.BLACK_STONE);
		board.setStone(1, 2, IBoard.BLACK_STONE);
		board.setStone(2, 2, IBoard.BLACK_STONE);

		ga = new GroupingAlgorithm(board);
		assertEquals(1, ga.getGroups().size());
		StonesGroup group = ga.getGroups().iterator().next();
		assertEquals(8, group.getLiberties().size());
	}
	
	
}
